package com.tomj.toolbox.auto;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.tomj.toolbox.auto.fileout.*;


import java.util.*;

/**
 * @Auther: tomj
 * @Date: 2019-03-05 15:56
 * @Description:
 */
public class GeneratorFactory {

    /**
     * 自动生成 controller service serviceimpl dao entity vo 的工具类
     * @return
     */
    public static AutoGenerator getInstance(Auto auto,Properties rb){
        //用来获取Mybatis-Plus.properties文件的配置信息
        //final ResourceBundle rb = ResourceBundle.getBundle("mybatis-plus");

        String className = auto.getDb();
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();
        //String className = dataSourceName;

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(rb.getProperty("OutputDir"));
        gc.setOpen(false);
        gc.setBaseResultMap(true);
        gc.setBaseColumnList(true);
        gc.setAuthor(rb.getProperty("author"));
        gc.setMapperName("%sDao");
        gc.setXmlName("%sMapper");
        gc.setServiceName("%sService");
        gc.setServiceImplName("%sServiceImpl");
        gc.setControllerName("%sController");
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL);
        dsc.setUrl(rb.getProperty("url"));
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername(rb.getProperty("userName"));
        dsc.setPassword(rb.getProperty("password"));
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent(rb.getProperty("parent"));  //com.bbgz.api.controller.admin
        pc.setController("api.controller." + className);
        pc.setService("service." + className);
        pc.setServiceImpl("service." + className + ".impl");
        pc.setEntity("domain." + className);
        pc.setMapper("dao." + className);
        mpg.setPackageInfo(pc);

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                Map<String,Object> map = new HashMap<>(8);
                map.put("voPackage",pc.getParent() + ".vo." + className); //设置生成vo的包名
                map.put("className",className);//希望模块名称与数据源名称一样
                map.put("operationType",auto.getBusType()); //设置操作类型的枚举
                this.setMap(map);
            }
        };
        List<Auto.AType> list = auto.getTypes();
        List<FileOutConfig> fileOutConfigs = new ArrayList<>();
        if(list.contains(Auto.AType.ENTITY)){
            FileOutConfig entity = new EntityOutConfig(rb,className);
            fileOutConfigs.add(entity);
        }

        if(list.contains(Auto.AType.VO)){
            FileOutConfig vo = new VOOutConfig(rb,className);
            fileOutConfigs.add(vo);
        }
        if(list.contains(Auto.AType.MAPPERXML)){
            FileOutConfig mapperXml = new MapperXmlOutConfig(rb,className);
            fileOutConfigs.add(mapperXml);
        }
        if(list.contains(Auto.AType.DAO)){
            FileOutConfig dao = new DaoOutConfig(rb,className);
            fileOutConfigs.add(dao);
        }
        if(list.contains(Auto.AType.SERVICE)){
            FileOutConfig serviceOutConfig = new ServiceOutConfig(rb,className);
            fileOutConfigs.add(serviceOutConfig);
        }
        if(list.contains(Auto.AType.SERVICEIMPL)){
            FileOutConfig serviceImplOutConfig = new ServiceImplOutConfig(rb,className);
            fileOutConfigs.add(serviceImplOutConfig);
        }

        if(list.contains(Auto.AType.CONTROLLER)){
            FileOutConfig controllerOutConfig = new ControllerOutConfig(rb,className);
            fileOutConfigs.add(controllerOutConfig);
        }



        cfg.setFileOutConfigList(fileOutConfigs);
        mpg.setCfg(cfg);
        mpg.setTemplate(new TemplateConfig().setXml(null));

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(false);
        strategy.setInclude(auto.getTableName());
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngineNew());
        return mpg;
    }






}
